package com.cvv.controller;


import com.alibaba.fastjson.JSONArray;
import com.cvv.dao.StationMapper;
import com.cvv.entity.Station;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("stapi")
@Controller
public class StationController {
    @Autowired
    private StationMapper stationMapper;
    @RequestMapping(value = "selectAll",method = RequestMethod.GET)
    @ResponseBody
    public String selectAll(){
      List<Station> list=stationMapper.selectAll();
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(list);
        String json = jsonArray.toString();
        return json;
    }
    
    @RequestMapping("getstbypage")
	public String getstbypage(Model model,HttpServletRequest request){
		String currentIndex = request.getParameter("pageIndex");
		String path = request.getParameter("path");
		if(path==null) {
			path = "../dist_hub/depot/depot_list.jsp";
		}
		int pageSize = 6;
		int pageIndex = 1;
		if(currentIndex!=null) {
			pageIndex=Integer.parseInt(currentIndex);
		}
		List<Station> listst = stationMapper.getstbypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = stationMapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listst", listst);
		String p = "forward:"+path+"";
		return p;
	}
	
	@RequestMapping("getstbypage2")
	public String getstbypage2(Model model,HttpServletRequest request){
	    String path = "../dist_hub/depot/depot_storage_list.jsp";
		int pageSize = 6;
		int pageIndex = 1;
		List<Station> listst = stationMapper.getstbypage((pageIndex-1)*pageSize, pageSize);
		model.addAttribute("pageIndex", pageIndex);
		int count = stationMapper.getTotalPage();
		int totalPage = count%pageSize==0?count/pageSize:count/pageSize+1;
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("listst", listst);
		String p = "forward:"+path+"";
		return p;
	}
	
	@RequestMapping("addst")
	public String addst(Model model,final String tex1,final String tex2,final String tex3,final String tex4) {
		Station st = new Station(tex1,tex2,tex3,tex4);
		int num = stationMapper.addst(st);
		return "redirect:/stapi/getstbypage";
	}
	
	@RequestMapping("getstbyid")
	public String getstbyid(Model model,HttpServletRequest request) {
		int stationId = Integer.valueOf(request.getParameter("stationId"));
		Station st = stationMapper.getstbyid(stationId);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot.jsp";
	}
	
	@RequestMapping("getstbyid2")
	public String getstbyid2(Model model,HttpServletRequest request) {
		int stationId = Integer.valueOf(request.getParameter("stationId"));
		Station st = stationMapper.getstbyid(stationId);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot_storage.jsp";
	}
	
	@RequestMapping("linkupdate")
	public String linkupdate(Model model,HttpServletRequest request) {
		int stationId = Integer.valueOf(request.getParameter("stationId"));
		Station st = stationMapper.getstbyid(stationId);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot_update.jsp";
	}
	
	@RequestMapping("linkupdate2")
	public String linkupdate2(Model model,final int tex1) {
		Station st = stationMapper.getstbyid(tex1);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot_update.jsp";
	}
	
	@RequestMapping("linkupdate3")
	public String linkupdate3(Model model,HttpServletRequest request) {
		int stationId = Integer.valueOf(request.getParameter("stationId"));
		Station st = stationMapper.getstbyid(stationId);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot_storage_update.jsp";
	}
	
	@RequestMapping("linkupdate4")
	public String linkupdate4(Model model,final int tex1) {
		Station st = stationMapper.getstbyid(tex1);
		model.addAttribute("station", st);
		return "forward:../dist_hub/depot/depot_storage_update.jsp";
	}
	
	@RequestMapping("updatestbyid")
	public String updatestbyid(Model model,final int tex1,final String tex2,final String tex3,final String tex4,final String tex5) {
        Station station = new Station(tex1,tex2,tex3,tex5,tex4);
        int num = stationMapper.updatestbyid(station);
		return "redirect:/stapi/getstbypage";
	}
	
	@RequestMapping("updatestbyid2")
	public String updatestbyid2(Model model,final int tex1,final int tex2) {
        Station station = new Station(tex1,tex2);
        int num = stationMapper.updatestbyid2(station);
		return "redirect:/stapi/getstbypage2";
	}
	
	@RequestMapping("delstation")
	public String delstation(Model model,HttpServletRequest request){
		int stationId = Integer.valueOf(request.getParameter("stationId"));
        int num = stationMapper.delstation(stationId);
		return "redirect:/stapi/getstbypage";
	}
	
	@RequestMapping("delstation2")
	public String delstation2(Model model,HttpServletRequest request){
		int stationId = Integer.valueOf(request.getParameter("stationId"));
        int num = stationMapper.delstation(stationId);
		return "redirect:/stapi/getstbypage2";
	}
}


